XML: harden parser against malformed input crashes#7555
Merged
Conversation
ANTLR error recovery synthesizes closing-tag tokens when input ends with an unclosed element, which caused `XmlParserVisitor` to throw `IndexOutOfBoundsException` from `advanceCursor` and `NullPointerException` when accessing `Name(1)`. Clamp `advanceCursor` to the source length and tolerate null `OPEN(1)`/`Name(1)`/`CLOSE(1)` so these inputs fall back to a `ParseError` (preserving original text) instead of crashing. See #7554 (comment)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes two crashes in
XmlParserVisitorwhen ANTLR's default error strategy synthesizes closing-tag tokens for malformed XML at EOF:advanceCursorno longer throwsIndexOutOfBoundsExceptionwhen a synthesized token would advance past the end of the source — it clamps tosource.length()instead.visitElementtolerates any combination of nullOPEN(1)/Name(1)/CLOSE(1)tokens so it no longer NPEs when error recovery couldn't produce them.After these changes, all three remaining
rewrite-xmlparse failures reported in "There were problems parsing" should show line number and/or excerpt of source code #7554 (comment) (MarcXmlParserTestSummaryAndKeywords.xml,MarcXmlParserTestArticle.xml,MedlineImporterTestMalformedEntry.xml) parse to aParseErrorthat preserves the original source text instead of throwing.The bare-ampersand and unterminated end-tag cases are intentionally malformed test fixtures — preserving them as a
ParseError(which round-trips the original text) is the documented fallback.Test plan
./gradlew :rewrite-xml:test— 3 new tests added inXmlParserTest, all rewrite-xml suites green